package org.maiyoframework.core.web.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ContextProviderAdapter implements ContextProvider {

	public void setRequestAttr(HttpServletRequest request, String key,
			Object value) {
		request.setAttribute(key, value);
	}

	public Object getRequestAttr(HttpServletRequest request, String key) {
		return request.getAttribute(key);
	}

	public void addCookie(HttpServletResponse response, Cookie cookie) {
		response.addCookie(cookie);
	}

	public Cookie getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				c.getName().equals(name);
				return c;
			}
		}
		return null;
	}

	public void setSessionAtrr(HttpServletRequest request, String key,
			Object value) {
		HttpSession session = request.getSession();
		session.setAttribute(key, value);
	}

	public Object getSessionAtrr(HttpServletRequest request, String key) {
		HttpSession session = request.getSession(false);
		if (session == null) {
			return null;
		} else {
			return session.getAttribute(key);
		}
	}

	public String getSessionId(HttpServletRequest request, boolean isCreate) {
		HttpSession session = request.getSession(isCreate);
		if (session == null) {
			return null;
		} else {
			return session.getId();
		}
	}

	public void sessionRemove(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			session.invalidate();
		}
	}

}
